//stock.cpp -- implementing the stock class
//version 00

//stock10.cpp -- Stock class with constructors, destructor added

//stock20.cpp -- augmented version

//stack10_10 --Stack member functions

#include <iostream>
#include "stock00.h"
#include "stock10.h"
#include "stock20.h"
#include "stack10_10.h"

Stack10_10::Stack10_10()	//create an empty stack
{
	top = 0;
}

bool Stack10_10::isempty() const
{
	return top == 0;
}

bool Stack10_10::isfull() const
{
	return top == MAX;
}

bool Stack10_10::push(const Item & item)
{
	if (top < MAX)
	{
		items[top++] = item;
		return true;
	}
	else
		return false;
}

bool Stack10_10::pop(Item & item)
{
	if (top > 0)
	{
		item = items[--top];
		return true;
	}
	else
		return false;
}

void Stock00::acquire(const std::string &co, long n, double pr)
{
	company = co;
	if (n < 0)
	{
		std::cout << "Number of shares can't be negative;"
			<< company << "shares set to to 0.\n";
		shares = 0;
	}
	else
		shares = n;
	share_val = pr;
	set_tot();
	return;
}

void Stock00::buy(long num, double price)
{
	if (num < 0)
	{
		std::cout << "Number of shares purchased can't be negative."
			<< "Transaction is aborted.\n";
	}
	else
	{
		shares += num;
		share_val = price;
		set_tot();
	}
	return;
}

void Stock00::sell(long num, double price)
{
	using std::cout;
	if (num < 0)
	{
		cout << "Number of shares sold can't be negative. "
			<< "Transaction is aborted.\n";
	}
	else if (num>shares)
	{
		cout << "You can't sell more than you have! "
			<< "Transaction is aborted.\n";
	}
	else
	{
		shares -= num;
		share_val = price;
		set_tot();
	}
}

void Stock00::update(double price)
{
	share_val = price;
	set_tot();
}

void Stock00::show()
{
	std::cout << "Company: " << company
		<< " Shares: " << shares << '\n'
		<< " Share Price: $" << share_val
		<< " Total Worth: $" << total_val << std::endl;
}

//constructors (verbose versions)
Stock10::Stock10()	//	default constructor
{
	std::cout << "Default constructor called\n";
	company = "no name";
	shares = 0;
	share_val = 0.0;
	total_val = 0.0;
}

Stock10::Stock10(const std::string &co, long n, double pr)
{
	std::cout << "Constructor using " << co << " called\n";
	company = co;

	if (n < 0)
	{
		std::cout << "Number of shares can't be negative; "
			<< company << " shares set to 0.\n";
		shares = 0;
	}
	else
		shares = n;
	share_val = pr;
	set_tot();
}

//class destructor
Stock10::~Stock10()		//verbose class destructor
{
	std::cout << "Bye, " << company << "!\n";
}

//other methods
void Stock10::buy(long num, double price)
{
	if (num < 0)
	{
		std::cout << "Number of shares purchased can't be negative. "
			<< "Transaction is aborted.\n";
	}
	else
	{
		shares += num;
		share_val = price;
		set_tot();
	}
}

void Stock10::sell(long num, double price)
{
	using std::cout;
	if (num < 0)
	{
		cout << "Number of shares sold can't be negative. "
			<< "Transaction is aborted.\n";
	}
	else if (num>shares)
	{
		cout << "You can't sell more than you have! "
			<< "Transaction is aborted.\n";
	}
	else
	{
		shares -= num;
		share_val = price;
		set_tot();
	}
}

void Stock10::update(double price)
{
	share_val = price;
	set_tot();
}

void Stock10::show()
{
	using std::cout;
	using std::ios_base;
	//set format to #.###
	ios_base::fmtflags orig =
		cout.setf(ios_base::fixed, ios_base::floatfield);
	std::streamsize prec = cout.precision(3);

	cout << "Company: " << company
		<< " Shares: " << shares << '\n';
	cout << "Share Price:$" << share_val;
	//set format to #.##
	cout.precision(2);
	cout << "  Total Worth: $" << total_val << std::endl;

	//restore original format
	cout.setf(orig, ios_base::floatfield);
	cout.precision(prec);
}

//constructors
Stock20::Stock20()		//default constructor
{
	company = "no name";
	shares = 0;
	share_val = 0.0;
	total_val = 0.0;
}

Stock20::Stock20(const std::string & co,long n , double pr)
{
	company = co;

	if (n < 0)
	{
		std::cout << "Number of shares can't be negative; "
			<< company << " shares set to 0.\n";
		shares = 0;
	}
	else
		shares = n;
	share_val = pr;
	set_tot();
}

//class destructor
Stock20::~Stock20()		//quiet class destructor
{
	using namespace std;
	cout << "bye bye" << endl;
}

//other methods
void Stock20::buy(long num, double price)
{
	if (num < 0)
	{
		std::cout << "Number of shares purchased can't be negative. "
			<< "Transaction is aborted.\n";
	}
	else
	{
		shares += num;
		share_val = price;
		set_tot();
	}
}

void Stock20::sell(long num, double price)
{
	using std::cout;
	if (num < 0)
	{
		cout << "Number of shares sold can't be negative. "
			<< "Transation is aborted.\n";
	}
	else if (num > shares)
	{
		cout << "You can't sell more than you have! "
			<< "Transaction is aborted.\n";
	}
	else
	{
		shares -= num;
		share_val = price;
		set_tot();
	}
}

void Stock20::update(double price)
{
	share_val = price;
	set_tot();
}

void Stock20::show() const
{
	using std::cout;
	using std::ios_base;
	//set format to #.###
	ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
	std::streamsize prec = cout.precision(3);

	cout << "Company: " << company << " Shares: " << shares << std::endl;
	cout << "  Share Price: $" << share_val;

	//set format to #.##
	cout.precision(2);
	cout << "  Total Worth: $" << total_val << '\n';

	//restoree original format
	cout.setf(orig, ios_base::floatfield);
	cout.precision(prec);
}

const Stock20 & Stock20::topval(const Stock20 & s) const
{
	if (s.total_val > total_val)
		return s;
	else
		return *this;
}


